package com.web.forum.tag;

import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;











import cn.mysteel.util.ObjectUtils;
import cn.mysteel.util.StringUtils;

import com.mysteel.page.Page;
import com.mysteel.page.PageService;
import com.web.kongjian.ao.utils.KongjianUtils;
import com.base.forum.entity.Forum;
import com.base.forum.entity.Invitation;
import com.base.forum.service.ForumService;
import com.base.forum.service.InvitationService;
import com.base.kongjian.entity.Comment;
import com.base.kongjian.service.CommentService;
import com.base.user.entity.Member;
import com.base.user.service.MemberService;
import com.mysteel.core.SpringContext;

public class ForumTag
{
	private InvitationService invitationService;
	private PageService pageService;
	private MemberService memberService;
	private CommentService commentService;
	private ForumService forumService;
	
	public ForumTag ()
	{
		invitationService = (InvitationService) SpringContext.getBean ("invitationService");
		pageService = (PageService) SpringContext.getBean ("pageService");
		memberService = (MemberService) SpringContext.getBean ("memberService");
		commentService = (CommentService) SpringContext.getBean ("commentService");
		forumService = (ForumService) SpringContext.getBean ("forumService");
	}
	
	public Page<Invitation> invitationTag(HttpServletRequest request,int maxPageRowCount)
	{
		long forumId = Long.parseLong (request.getParameter ("forumId"));
		
		Map<String, Object> params = new HashMap<String, Object>();
		params.put ("forumId" ,forumId);
		int currentPage = 1;
		String pageNumStr = request.getParameter ("page");
		if(!StringUtils.isTrimEmpty (pageNumStr))
		{
			currentPage = Integer.parseInt (pageNumStr);
		}
		
		Page<Invitation> page = null;
		page = invitationService.query (params ,currentPage ,maxPageRowCount);
		StringBuffer url = new StringBuffer ("?page=${pageNum}&forumId="+forumId);
		if(ObjectUtils.notNull (page))
		{
			page.setURL (new String(url));
			page.setTemplate ("pshow");
			pageService.build (page);
		}
		
		return page;
	}
	
	
	public Map<String, Object> replyTag(HttpServletRequest request)
	{
		Map<String, Object> returnMap = new HashMap<String,Object>();
		long memberId = Long.parseLong (request.getParameter ("comment_MId"));
		Member member = memberService.getMemberById (memberId);
		
		if(ObjectUtils.notNull (member))
		{
			returnMap.put ("nickName" ,member.getNickName ());
		}
		
		returnMap.put ("comment_MId" ,memberId);
		long workId = Long.parseLong (request.getParameter ("workId"));
		long forumId = Long.parseLong (request.getParameter ("forumId"));
		
		returnMap.put ("workId" ,workId);
		returnMap.put ("forumId" ,forumId);
		
		Map<String, Object> params = new HashMap<String,Object> ();
		params.put ("workId" ,workId);
		params.put ("workKind" ,4);
		Page<Comment> page = commentService.queryComment (params ,0 ,-1);
		if(ObjectUtils.notNull (page))
		{
			returnMap.put ("commentId" ,page.getDatas ().get (0).getId ());
		}
		
		return returnMap;
	}
	
	
	public String messageTag(HttpServletRequest request)
	{
		
		String string = request.getParameter ("message");
		try
		{
			string = new String(string.getBytes ("iso8859-1"),"UTF-8");
		}catch (UnsupportedEncodingException e)
		{
			e.printStackTrace();
		}
		
		return string;
	}
	
	
	public Page<Forum> mapTag( int maxPageRowCount,HttpServletRequest request)
	{
		Page<Forum> page = null;
		
		int currentPage = 1;
		String str = request.getParameter ("page");
		if(!StringUtils.isTrimEmpty (str))
		{
			currentPage = Integer.parseInt (str);
		}
		
		page = forumService.query (null ,currentPage ,maxPageRowCount);
		
		if(ObjectUtils.notNull (page))
		{
			StringBuffer url = new StringBuffer ("?page=${pageNum}");
			page.setURL (new String(url));
			pageService.build (page);
		}
		
		return page;
	}
	
	/**
	 * ��̳��ϸ
	 * @param request
	 * @return
	 */
	public Map<String, Object> forumInforTag(HttpServletRequest request)
	{
		Map<String,Object> inforMap = new HashMap<String,Object>();
		long forumId = Long.parseLong (request.getParameter ("forumId"));
		Forum f = forumService.getById (forumId);
		if(ObjectUtils.notNull (f))
		{
			inforMap.put ("forum" ,f);
		}
		
		return inforMap;
	}
	
	public String isMember(HttpServletRequest request)
	{
		long memberId = KongjianUtils.getMemberIdByCookie (request);
		
		long forumId = Long.parseLong (request.getParameter ("forumId"));
		Forum f = forumService.getById (forumId);
		
		if(ObjectUtils.notNull (f))
		{
			String memberStr = f.getMemberId ();
			
			if(memberStr.indexOf (memberId+",")!=-1)
			{
				
				return "exit";
			}
		}
		
		return "no_exit";
	}
	
}
